package com.jh.listener;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.google.gson.Gson;
import com.jh.comm.RXBQTakeThread;
import com.jh.comm.RadarTCPRXThread;
import com.jh.comm.RadarTCPTakeTXBQThread;
import com.jh.pojo.Entity;
import com.jh.pojo.IPCamMsg;
import com.jh.pojo.IPCamRadar;
import com.jh.pojo.IPCamRadarInfo;
import com.jh.pojo.RadarMsg;
import com.jh.pojo.SPInfo;
import com.jh.pojo.cmd.MulitResideCmdBean;
import com.jh.pojo.cmd.ResideParaBean;
import com.jh.service.impl.IPCamRadarInfoSrvImpl;
import com.jh.utils.BlockingQueueUtils;
import com.jh.utils.CacheUtils;
import com.jh.utils.GetConfig;
import com.jh.utils.SocketUtils;

/**
 * Application Lifecycle Listener implementation class Conn
 *
 */
@WebListener
public class ConnSCL implements ServletContextListener {

    private RadarTCPRXThread radarTCPRXThread;

    private RadarTCPTakeTXBQThread radarTCPTakeTXBQThread;

    /**
     * Default constructor.
     */
    public ConnSCL() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce) {

        if (radarTCPRXThread != null && radarTCPRXThread.isInterrupted()) {
            radarTCPRXThread.interrupt();
            SocketUtils.closeSocket();
            System.out.println("SocketUtils.closeSocket!");
        }
    }

    /**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce) {

        /* 启动接收雷达数据的线程，将来自雷达的数据放入接收队列*/
        if (radarTCPRXThread == null) {
            RadarTCPRXThread radarTCPRXThread = new RadarTCPRXThread();
            radarTCPRXThread.start();

        }
        if (radarTCPTakeTXBQThread == null) {
            RadarTCPTakeTXBQThread radarTCPTakeTXBQThread = new RadarTCPTakeTXBQThread();
            radarTCPTakeTXBQThread.start();
        }

        sce.getServletContext().setAttribute("USER_PARAM1", "liuzhneg");
        sce.getServletContext().setAttribute("USER_PARAM2", 2);

        CacheUtils cacheUtils = CacheUtils.getInstance();
        /*从mysql中获取设备列表*/
        IPCamRadarInfoSrvImpl srv = new IPCamRadarInfoSrvImpl();
        List<IPCamRadarInfo> list = srv.list(null, true);
        /*初始化雷达和摄像头缓存映射，并填入部分信息*/
        for (IPCamRadarInfo ir : list) {
            String spDesc = ir.getSp_name();
            SPInfo spInfo = new SPInfo(spDesc);
            IPCamMsg ipCamMsg = new IPCamMsg(ir.getSp_name(), ir.getIpcam_ip());
            RadarMsg radarMsg = new RadarMsg(ir.getSp_name(), ir.getRadar_ip());
            MulitResideCmdBean mulitResideCmdBean = new MulitResideCmdBean();
            ResideParaBean resideParaBean1 = new ResideParaBean();
            ResideParaBean resideParaBean2 = new ResideParaBean();
            ResideParaBean resideParaBean3 = new ResideParaBean();
            ResideParaBean resideParaBean4 = new ResideParaBean();
            ResideParaBean resideParaBean5 = new ResideParaBean();
            ResideParaBean resideParaBean6 = new ResideParaBean();
            ResideParaBean resideParaBean7 = new ResideParaBean();
            ResideParaBean resideParaBean8 = new ResideParaBean();
            ResideParaBean resideParaBean9 = new ResideParaBean();
            ResideParaBean resideParaBean10 = new ResideParaBean();
            mulitResideCmdBean.setReside1(resideParaBean1);
            mulitResideCmdBean.setReside2(resideParaBean2);
            mulitResideCmdBean.setReside3(resideParaBean3);
            mulitResideCmdBean.setReside4(resideParaBean4);
            mulitResideCmdBean.setReside5(resideParaBean5);
            mulitResideCmdBean.setReside6(resideParaBean6);
            mulitResideCmdBean.setReside7(resideParaBean7);
            mulitResideCmdBean.setReside8(resideParaBean8);
            mulitResideCmdBean.setReside9(resideParaBean9);
            mulitResideCmdBean.setReside10(resideParaBean10);

            radarMsg.setrMrcBean(mulitResideCmdBean);

            spInfo.setIpCamMsg(ipCamMsg);
            spInfo.setRadarMsg(radarMsg);
            cacheUtils.add((Object) ir.getRadar_ip(), (Object) spInfo);
        }

        // 通知前端的信息，数据是否初始化成功
        // sce.getServletContext().setAttribute("CACHE_UTILS", cacheUtils);
        /*启动一个线程，从接收队列取出来自TCP通信的数据，转发至WS服务端*/
        RXBQTakeThread rxbqTakeThread = new RXBQTakeThread();
        rxbqTakeThread.start();
        GetConfig configInstance = GetConfig.getInstance();
        configInstance.init();
        System.out.println("WsUrl: " + configInstance.getWsUrl());
        sce.getServletContext().setAttribute("WSURL", configInstance.getWsUrl());

        // 发送设备信息
        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();
        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        Gson gson = new Gson();
        Entity entity = new Entity();
        IPCamRadar ipcamRadar = new IPCamRadar();

        for (IPCamRadarInfo iri : list) {
            ipcamRadar.setRadarIP(iri.getRadar_ip());
            ipcamRadar.setRadarPort(Integer.parseInt(iri.getRadar_port()));
            ipcamRadar.setIpcamIP(iri.getIpcam_ip());
            ipcamRadar.setIpcamPort(8000);
            ipcamRadar.setName(iri.getIpcam_name());
            ipcamRadar.setPwd(iri.getIpcam_pwd());
            ipcamRadar.setRtsp(iri.getIpcam_url());
            entity.setType(25); // 查看协议
            entity.setData(ipcamRadar);
            String json = gson.toJson(entity);
            try {
                txBq.put(json);
                // Thread.sleep(500);
            } catch (InterruptedException e) {

                System.out.println("txBq put failed!");

            }
        }
    }

}
